#include <asm/i386.h>
#include <cos/protect.h>
#include <cos/kernel.h>
#include <cos/sched.h>
#include <cos/mm.h>
#include <unistd.h>

void sys_print(const char *str);

void sys_halt(void)
{
	sti(); // enable the interrupts
	hlt(); // stop cpu and wait for interrupt to restart...
}

void sys_showclock(void)
{
	showtime();
}

// eax --> system_call_nr
// ebx --> first arg
// ecx --> second arg
// edx --> third arg

int do_syscall(struct context_struct * ucontextp, int call_type)
{
	long retval = SUCCESS;
	procs.running_proc->reenter ++;
	// do something here...
	
	switch(ucontextp->eax){
	case __NR_sendrec:
		retval = sys_sendrec((long)ucontextp->ebx, (struct msg_struct *)ucontextp->ecx);
		break;
	case __NR_send:
		retval = sys_send((long)ucontextp->ebx, (struct msg_struct *)ucontextp->ecx, SEND);
		break;
	case __NR_receive:
		retval = sys_receive((long)ucontextp->ebx, (struct msg_struct *)ucontextp->ecx);
		break;
	case __NR_notify:
		retval = sys_notify(procs.running_proc->pid, (long)ucontextp->ebx);
		break;
	case __NR_halt:
		sys_halt();
		break;
	case __NR_showclock:
		sys_showclock();
		break;
	case __NR_user_print:
		sys_print((const char *)ucontextp->ebx);
		break;
	default:
		printk("unknown syscall %d, from pid %d\n\r", ucontextp->eax, procs.running_proc->pid);
		retval = FAILURE;
	}
	ucontextp->eax = retval;
	procs.running_proc->reenter --;
	// maybe a high priority proc has been ready for running......
	schedule();
	return retval;
}
